package com.hit.dissim.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hit.dissim.algorithm.ARss;
import com.hit.dissim.algorithm.BaseUtil;
import com.hit.dissim.algorithm.RssAlg;
import com.hit.dissim.algorithm.findGenes;
import com.opensymphony.xwork2.ActionSupport;

public class FooAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id1;		//术语id
	private String id2;	
	private String res;		//计算结果
	private int type1;		//术语类型
	private int type2;
	private int selected;	//计算方式，0为Rss，1为ARss
	
	private JSONObject data;
	private Map<String,Object> dataMap = new HashMap<String,Object>();

	public String execute() {
		getText("libai");	//这个getText真的是个bug，忘记他是干嘛的了
		return SUCCESS;
	}
	
	/**
	 * 传入两个id，计算他们的Rss值并返回
	 * @return
	 */
	public String getRss(){
		RssAlg rss = new RssAlg();
		double Rs = rss.getResult(id1, id2);
//		double cod = rss.getCod("GO:0042310", "GO:0060004");
		
		Double std = BaseUtil.saveRsandStandardize("rss", id1, id2, Rs);
		Double zScore = BaseUtil.getZscore("rss", id1, id2, Rs);
		
		data = new JSONObject();
		//由于“JSON does not allow non-finite numbers”，所以传一个String回去吧
		data.put("std", std.toString());
		data.put("zscore", zScore.toString());
		res = String.valueOf(std);
//		System.out.println("hello");
		return SUCCESS;
	}
	
	/**
	 * 传入两个id，计算他们的ARss值并返回
	 * @return
	 */
	public String getARss() {
		res = "ARss";
		ARss arss = new ARss(this.id1, this.id2);	//参数在这里传递
//		ARss arss = new ARss("GO:0042310", "GO:0005622",1,2);
		double ARs = arss.getResult();
		
		Double std = BaseUtil.saveRsandStandardize("arss", id1, id2, ARs);
		Double zScore = BaseUtil.getZscore("arss", id1, id2, ARs);
		res = String.valueOf(std);
		data = new JSONObject();
		
		data.put("std", std.toString());
		data.put("zscore", zScore.toString());
		return SUCCESS;
	}
	
	/**
	 * 传入两个id，得到他们的附属gene，返回两个list
	 * @return
	 */
	public String getGenes(){
		System.out.println("getGenes");
		dataMap.clear();
		List[] res = findGenes.find(id1, id2);
		data = new JSONObject();
		JSONArray jsonlist1 = JSONArray.fromObject(res[0]);
		JSONArray jsonlist2 = JSONArray.fromObject(res[1]);
		data.put("list1", jsonlist1);
		data.put("list2", jsonlist2);
//		dataMap.put("list1", jsonlist1);
//		dataMap.put("list2", jsonlist2);
//		data.put("list1","1");
//		data.put("list2","2");
		
		return SUCCESS;
	}

	public String getId1() {
		return id1;
	}

	public void setId1(String id1) {
		this.id1 = id1;
	}

	public String getId2() {
		return id2;
	}

	public void setId2(String id2) {
		this.id2 = id2;
	}

	public String getRes() {
		return res;
	}

	public void setRes(String res) {
		this.res = res;
	}
	public int getType1() {
		return type1;
	}

	public void setType1(int type1) {
		this.type1 = type1;
	}

	public int getType2() {
		return type2;
	}

	public void setType2(int type2) {
		this.type2 = type2;
	}

	public int getSelected() {
		return selected;
	}

	public void setSelected(int selected) {
		this.selected = selected;
	}
	
	public JSONObject getData() {
		return data;
	}

	public void setData(JSONObject data) {
		this.data = data;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}
	
}
